package com.ecommerce.service;

import com.ecommerce.dao.UserDAO;
import com.ecommerce.model.User;
import com.ecommerce.util.InputUtil;
import java.util.List;

public class UserService {
    private UserDAO userDAO;

    public UserService() {
        this.userDAO = new UserDAO();
    }

    public boolean register(String username, String password, String email) {
        if (username.isEmpty() || password.isEmpty()) {
            System.out.println("用户名和密码不能为空！");
            return false;
        }

        if (userDAO.isUsernameExists(username)) {
            System.out.println("用户名已存在！");
            return false;
        }

        User user = new User(username, password, email, "customer");
        boolean success = userDAO.registerUser(user);

        if (success) {
            System.out.println("注册成功！");
        } else {
            System.out.println("注册失败！");
        }
        return success;
    }

    public User login(String username, String password) {
        if (username.isEmpty() || password.isEmpty()) {
            System.out.println("用户名和密码不能为空！");
            return null;
        }

        User user = userDAO.login(username, password);
        if (user != null) {
            System.out.println("登录成功！欢迎，" + user.getUsername() + "！");
            if (isAdmin(user)) {
                System.out.println("您当前是管理员身份。");
            }
        } else {
            System.out.println("登录失败！用户名或密码错误。");
        }
        return user;
    }

    public void manageUsers(User currentAdmin) {
        while (true) {
            System.out.println("\n=== 用户管理 ===");
            System.out.println("1. 查看所有顾客");
            System.out.println("2. 查看所有管理员");
            System.out.println("3. 返回上级菜单");

            int choice = InputUtil.getIntInput("请选择操作: ");

            switch (choice) {
                case 1:
                    displayCustomers();
                    break;
                case 2:
                    displayAdmins();
                    break;
                case 3:
                    return;
                default:
                    System.out.println("无效选择，请重新输入！");
            }
        }
    }

    public void displayCustomers() {
        List<User> customers = userDAO.getCustomers();
        System.out.println("\n=== 顾客列表 ===");
        if (customers.isEmpty()) {
            System.out.println("暂无顾客");
        } else {
            System.out.println("ID | 用户名 | 邮箱 | 注册时间");
            System.out.println("----------------------------------------");
            for (User customer : customers) {
                System.out.printf("%-3d | %-10s | %-15s | %s\n",
                        customer.getId(), customer.getUsername(),
                        customer.getEmail(), customer.getCreatedAt());
            }
        }
        System.out.println("总计: " + customers.size() + " 名顾客");
        System.out.println("========================================");
    }

    public void displayAdmins() {
        List<User> admins = userDAO.getAdmins();
        System.out.println("\n=== 管理员列表 ===");
        if (admins.isEmpty()) {
            System.out.println("暂无其他管理员");
        } else {
            System.out.println("ID | 用户名 | 邮箱 | 注册时间");
            System.out.println("----------------------------------------");
            for (User admin : admins) {
                System.out.printf("%-3d | %-10s | %-15s | %s\n",
                        admin.getId(), admin.getUsername(),
                        admin.getEmail(), admin.getCreatedAt());
            }
        }
        System.out.println("总计: " + admins.size() + " 名管理员");
        System.out.println("========================================");
    }

    public void displayMyInfo(User user) {
        System.out.println("\n=== 我的信息 ===");
        System.out.println(user.toDetailedString());
        System.out.println("================");
    }

    public boolean isAdmin(User user) {
        return user != null && "admin".equals(user.getRole());
    }
}